package com.ruoyi.production.service.impl.plug;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.plug.PlugAmount;
import com.ruoyi.production.domain.plug.PlugPurchase;
import com.ruoyi.production.domain.plug.PlugAmount;
import com.ruoyi.production.mapper.plug.PlugAmountMapper;
import com.ruoyi.production.mapper.plug.PlugAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.plug.PlugPurchaseMapper;
import com.ruoyi.production.domain.plug.PlugPurchase;
import com.ruoyi.production.service.plug.IPlugPurchaseService;

/**
 * 穴盘购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class PlugPurchaseServiceImpl implements IPlugPurchaseService
{
    @Autowired
    private PlugPurchaseMapper plugPurchaseMapper;
    @Autowired
    private PlugAmountMapper plugAmountMapper;


    /**
     * 查询穴盘购买信息
     *
     * @param plugPurchaseId 穴盘购买信息ID
     * @return 穴盘购买信息
     */
    @Override
    public PlugPurchase selectPlugPurchaseById(Long plugPurchaseId)
    {
        return plugPurchaseMapper.selectPlugPurchaseById(plugPurchaseId);
    }

    /**
     * 查询穴盘购买信息列表
     *
     * @param plugPurchase 穴盘购买信息
     * @return 穴盘购买信息
     */
    @Override
    public List<PlugPurchase> selectPlugPurchaseList(PlugPurchase plugPurchase)
    {
        return plugPurchaseMapper.selectPlugPurchaseList(plugPurchase);
    }

    /**
     * 新增穴盘购买信息
     *
     * @param plugPurchase 穴盘购买信息
     * @return 结果
     */
    @Override
    public int insertPlugPurchase(PlugPurchase plugPurchase)
    {
        return plugPurchaseMapper.insertPlugPurchase(plugPurchase);
    }

    /**
     * 修改穴盘购买信息
     *
     * @param plugPurchase 穴盘购买信息
     * @return 结果
     */
    @Override
    public int updatePlugPurchase(PlugPurchase plugPurchase)
    {
        Long plugPurchaseId = plugPurchase.getPlugPurchaseId();
        //数据库存储的数据
        PlugPurchase PurchaseDB = selectPlugPurchaseById(plugPurchaseId);
        //数据库存储的原数据的总量
        PlugAmount originAmounts = new PlugAmount();
        originAmounts.setOriginPlace(PurchaseDB.getOriginPlace());
        originAmounts.setPlugTypeId(PurchaseDB.getPlugTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setBrand(PurchaseDB.getBrand());
        List<PlugAmount> amounts = plugAmountMapper.selectPlugAmountList(originAmounts);
        PlugAmount originAmount = amounts.get(0);
        if (PurchaseDB.getOriginCompany().equals(plugPurchase.getOriginCompany()) &&
                PurchaseDB.getPlugTypeId().equals(plugPurchase.getPlugTypeId()) &&
                PurchaseDB.getOriginPlace().equals(plugPurchase.getOriginPlace()) &&
                PurchaseDB.getBrand().equals(plugPurchase.getBrand())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+plugPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            plugAmountMapper.updatePlugAmount(originAmount);
        }else {
            PlugAmount newAmounts = new PlugAmount();
            newAmounts.setOriginPlace(plugPurchase.getOriginPlace());
            newAmounts.setPlugTypeId(plugPurchase.getPlugTypeId());
            newAmounts.setOriginCompany(plugPurchase.getOriginCompany());
            newAmounts.setBrand(plugPurchase.getBrand());
            List<PlugAmount> amounts1 = plugAmountMapper.selectPlugAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(plugPurchase.getPurchaseAmount());
                plugAmountMapper.insertPlugAmount(newAmounts);
            }else {
                PlugAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+plugPurchase.getPurchaseAmount());
                plugAmountMapper.updatePlugAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            plugAmountMapper.updatePlugAmount(originAmount);
        }
        return plugPurchaseMapper.updatePlugPurchase(plugPurchase);
    }

    /**
     * 批量删除穴盘购买信息
     *
     * @param plugPurchaseIds 需要删除的穴盘购买信息ID
     * @return 结果
     */
    @Override
    public int deletePlugPurchaseByIds(Long[] plugPurchaseIds)
    {
        for (Long plugPurchaseId : plugPurchaseIds) {
            PlugPurchase plugPurchase = selectPlugPurchaseById(plugPurchaseId);
            PlugAmount plugAmount = new PlugAmount();
            plugAmount.setOriginPlace(plugPurchase.getOriginPlace());
            plugAmount.setOriginCompany(plugPurchase.getOriginCompany());
            plugAmount.setPlugTypeId(plugPurchase.getPlugTypeId());
            List<PlugAmount> amounts = plugAmountMapper.selectPlugAmountList(plugAmount);
            PlugAmount plugAmount1 = amounts.get(0);
            plugAmount1.setStorageAmount(plugAmount1.getStorageAmount() - plugPurchase.getPurchaseAmount());
            plugAmountMapper.updatePlugAmount(plugAmount1);
        }
        return plugPurchaseMapper.deletePlugPurchaseByIds(plugPurchaseIds);
    }

    /**
     * 删除穴盘购买信息信息
     *
     * @param plugPurchaseId 穴盘购买信息ID
     * @return 结果
     */
    @Override
    public int deletePlugPurchaseById(Long plugPurchaseId)
    {
        PlugPurchase plugPurchase = selectPlugPurchaseById(plugPurchaseId);
        PlugAmount plugAmount = new PlugAmount();
        plugAmount.setOriginPlace(plugPurchase.getOriginPlace());
        plugAmount.setPlugTypeId(plugPurchase.getPlugTypeId());
        plugAmount.setOriginCompany(plugPurchase.getOriginCompany());
        plugAmount.setBrand(plugPurchase.getBrand());
        List<PlugAmount> amounts = plugAmountMapper.selectPlugAmountList(plugAmount);
        PlugAmount plugAmount1 = amounts.get(0);
        plugAmount1.setStorageAmount(plugAmount1.getStorageAmount() - plugPurchase.getPurchaseAmount());
        plugAmountMapper.updatePlugAmount(plugAmount1);
        return plugPurchaseMapper.deletePlugPurchaseById(plugPurchaseId);
    }

    /**
     * 插入穴盘购买表更新穴盘量表
     *
     * @param plugPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(PlugPurchase plugPurchase) {
        PlugAmount plugAmount = new PlugAmount();
        plugAmount.setCreateTime(plugPurchase.getCreateTime());
        plugAmount.setCreateBy(plugPurchase.getCreateBy());
        plugAmount.setUpdateTime(plugPurchase.getUpdateTime());
        plugAmount.setUpdateBy(plugPurchase.getUpdateBy());
        plugAmount.setOriginPlace(plugPurchase.getOriginPlace());
        plugAmount.setPlugTypeId(plugPurchase.getPlugTypeId());
        plugAmount.setOriginCompany(plugPurchase.getOriginCompany());
        plugAmount.setBrand(plugPurchase.getBrand());
        List<PlugAmount> plugAmounts = plugAmountMapper.selectPlugAmountList(plugAmount);
        plugAmount.setStorageAmount(plugPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(plugAmounts)) {
            PlugAmount amount = plugAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + plugPurchase.getPurchaseAmount());
            plugAmountMapper.updatePlugAmount(amount);
        } else {
            plugAmountMapper.insertPlugAmount(plugAmount);
        }
        return insertPlugPurchase(plugPurchase);
    }
}
